package com.foryou.wechat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.foryou.wechat.common.Constants;
import com.foryou.wechat.common.enums.AuditStatusEnum;
import com.foryou.wechat.common.enums.CapacityEnum;
import com.foryou.wechat.common.enums.ImageTypeEnum;
import com.foryou.wechat.common.enums.InviteTypeEnum;
import com.foryou.wechat.entity.ProductionCollectEntity;
import com.foryou.wechat.entity.ProductionEntity;
import com.foryou.wechat.entity.ShootEntity;
import com.foryou.wechat.mapper.ProductionCollectMapper;
import com.foryou.wechat.mapper.ProductionMapper;
import com.foryou.wechat.mapper.SubjectMapper;
import com.foryou.wechat.model.dto.ProductionDTO;
import com.foryou.wechat.model.Query;
import com.foryou.wechat.model.vo.ProductionVo;
import com.foryou.wechat.model.vo.UserVo;
import com.foryou.wechat.model.vo.WorksActionVo;
import com.foryou.wechat.service.ProductionService;
import com.foryou.wechat.service.UserFollowService;
import com.foryou.wechat.service.UserImageService;
import com.foryou.wechat.service.UserService;
import com.foryou.wechat.utils.EnumUtil;
import com.foryou.wechat.utils.HttpContextUtils;
import com.foryou.wechat.utils.RelativeDateFormat;
import com.foryou.wechat.utils.Response;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by Lee.
 */
@Service
public class ProductionServiceImpl implements ProductionService {

    private final ProductionMapper productionMapper;

    private final SubjectMapper subjectMapper;

    private final UserImageService imageService;

    private final UserFollowService userFollowService;

    private final ProductionCollectMapper productionCollectMapper;

    private final UserService userService;

    public ProductionServiceImpl(UserImageService imageService, ProductionMapper productionMapper, SubjectMapper subjectMapper, UserFollowService userFollowService, ProductionCollectMapper productionCollectMapper, UserService userService) {
        this.imageService = imageService;
        this.productionMapper = productionMapper;
        this.subjectMapper = subjectMapper;
        this.userFollowService = userFollowService;
        this.productionCollectMapper = productionCollectMapper;
        this.userService = userService;
    }

    @Override
    @Transactional
    public Response addProduction(ProductionDTO productionDTO) {

        Long userSessionId = HttpContextUtils.getUserId();
        boolean checkResult = userService.checkUserInfo(userSessionId);
        if (!checkResult) {
            return Response.failure(Constants.EDIT_USER_INFO);
        }
        ProductionEntity productionEntity = new ProductionEntity(
                productionDTO.getName(),
                productionDTO.getIntro(),
                productionDTO.getEquipment(),
                productionDTO.getShootAddress(),
                productionDTO.getReturnFilm(),
                productionDTO.getIsOneself(),
                productionDTO.getSubjectIds(),
                userSessionId
        );
        int result = productionMapper.insert(productionEntity);
        if (result > 0) {
            return Response.success(productionEntity.getId());
        }
        return Response.failure(Constants.OPERATION_FAILURE);
    }

    @Override
    public Response getProductionDetail(Long productionId) {
        ProductionEntity productionEntity = productionMapper.selectById(productionId);
        if (ObjectUtils.isEmpty(productionEntity)) {
            return Response.failure(Constants.OPERATION_FAILURE);
        }
        //1.得到userVo对象
        UserVo userVo = userFollowService.getUserVo(productionEntity.getUserId());
        //2.查询图片集合URL
        List<String> imageUrlList = imageService.imageUrlList(productionId, ImageTypeEnum.WORKS);
        //3.得到主题标签集合
        List<String> subjectList = subjectMapper.getSubjectList(productionEntity.getSubjectIds().split(","));
        //4.查询是否已经收藏该作品
        ProductionCollectEntity productionCollectEntity = productionCollectMapper.queryUserIsCollect(productionId,HttpContextUtils.getUserId());
        boolean isCollect = !ObjectUtils.isEmpty(productionCollectEntity);
        boolean isOwner = productionEntity.getUserId().equals(HttpContextUtils.getUserId());
        //5.组装数据
        return Response.success(
                new ProductionVo(
                        productionId,
                        imageUrlList,
                        productionEntity.getName(),
                        productionEntity.getIntro(),
                        productionEntity.getEquipment(),
                        productionEntity.getReturnFilm() == 0,
                        productionEntity.getIsOneself() == 0,
                        subjectList,
                        userVo,
                        isCollect,
                        DateFormatUtils.format(productionEntity.getCreateTime(), Constants.DATE_FORMAT_PATTERN_CH),
                        isOwner,
                        productionEntity.getScore(),
                        productionEntity.getAuditStatus()
                )
        );
    }

    @Override
    public Response getProductionList(Query query) {
        List<WorksActionVo> worksActionVoList = productionMapper.queryProductionList(query);
        productionActionListHandler(worksActionVoList);
        return Response.success(worksActionVoList);
    }

    @Override
    public Response collectProduction(Long productionId) {
        productionCollectMapper.insert(new ProductionCollectEntity(productionId, HttpContextUtils.getUserId()));
        return Response.success();
    }

    @Override
    public Response queryCollectProduction(int start, Long userId) {
        List<WorksActionVo> worksActionVoList = productionMapper.queryCollectProductionList(start, userId);
        productionActionListHandler(worksActionVoList);
        return Response.success(worksActionVoList);
    }

    private void productionActionListHandler(List<WorksActionVo> worksActionVoList) {
        worksActionVoList.forEach(e->{
            e.setType(InviteTypeEnum.WORKS.getCode());
            e.setCapacity(EnumUtil.getEnumByCode(CapacityEnum.class,Integer.valueOf(e.getCapacity())));
            e.setImageUrlList(imageService.imageUrlList(e.getBusinessId(), ImageTypeEnum.WORKS));
            e.setSubjectList(subjectMapper.getSubjectList(e.getSubjectIds().split(",")));
            e.setTime(RelativeDateFormat.format(e.getCreateTime()));
        });
    }

    @Override
    public Response cancelProduction(Long productionId) {
        productionCollectMapper.deleteProductionClock(productionId, HttpContextUtils.getUserId());
        return Response.success();
    }

}
